package xx.book.spider.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import xx.book.spider.entity.Book;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Component
public class HandleProcessor implements PageProcessor {

    @Autowired
    private HandlepipeLine handlepipeLine;

    public static String url = "https://book.sciencereading.cn/shop/book/Booksimple/list.do?showQueryModel.dp1Value=f4fe63b3889211e7a2df00163e2ed6f9";
    public static String imgUrlPrefix = "https://book.sciencereading.cn";


    private Site site = Site.me()
            .setCharset("UTF-8")//设置编码
            .setTimeOut(10 * 1000)//设置超时时间
            .setRetrySleepTime(3000)//设置重试的间隔时间
            .setRetryTimes(3)//设置重试的次数
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36")//设置请求头
            ;

    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        List<Selectable> bookNodes = html.xpath("//div[@class='book_content row']").nodes();
        if (bookNodes != null && bookNodes.size() > 0) {
            List<Book> bookList = new ArrayList<>();
            for (Selectable item : bookNodes
            ) {
                Book book = saveBookInfo(item);
                bookList.add(book);
            }
            page.putField("bookInfoList", bookList);
        }
    }


    @Override
    public Site getSite() {
        return site;
    }


    private Book saveBookInfo(Selectable bookNode) {
        Book book = new Book();
        String bookImg = bookNode.xpath("//img[@class='img-responsive']/@src").toString();
        log.info("书籍图片地址 ---->[{}]", bookImg);
        book.setBookImgUrl(imgUrlPrefix+bookImg);

        String bookName = bookNode.xpath("//b[@class='kc_title keyContent']/text()").toString();
        log.info("书籍名称 ---->[{}]", bookName);
        book.setBookName(bookName);

        String author = bookNode.xpath("//span[@class='kc_author keyContent book_list_title_right lwq_info_right']/text()").toString();
        log.info("作者 ---->[{}]", author);
        book.setAuthor(author);

        String isbn = bookNode.xpath("//span[@class='kc_ISBN keyContent']/text()").toString();
        log.info("isbn ---->[{}]", isbn);
        book.setIsbn(isbn);

        String press = bookNode.xpath("//span[@class='keyContent book_list_title_right lwq_info_right']/text()").toString();
        log.info("出版社 ---->[{}]", press);
        book.setPress(press);

//        String publishDate = bookNode.xpath("//span[@class='book_list_title_block']/next()/text()").toString();
//        log.info("出版日期 ---->[{}]",publishDate);
//        book.setPublishDate(parse(publishDate,"yyyy-MM"));
        return book;
    }


    public static final Date parse(String stringValue, String formatPattern) {
        if (stringValue != null && formatPattern != null) {
            Date finalDate = null;
            try {
                finalDate = DateUtils.parseDate(stringValue, new String[]{formatPattern});
            } catch (ParseException var4) {
                log.error("解析日期出错", var4);
            }
            return finalDate;
        } else {
            return null;
        }
    }


    public void process() {
        Spider.create(new HandleProcessor())
                // 初始访问url地址
                .addUrl(url)
                .thread(5)
                .addPipeline(this.handlepipeLine)
                .run();
    }
}
